// UserDefinedDisplayQueryHandler.cpp: implementation of the UserDefinedDisplayQueryHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "UserDefinedDisplayQueryHandler.h"
#include "UserDefinedDisplaySet.h"
#include "DatabaseManager.h"

#include "CommonLib\Structure.h"
#include "CommonLib\Packet.h"

#include "CommServerLib\CommWithClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

UserDefinedDisplayQueryHandler::UserDefinedDisplayQueryHandler(const Packet& packet, HANDLE hClient):
	BaseRequestHandler(packet, hClient)
{
}

UserDefinedDisplayQueryHandler::~UserDefinedDisplayQueryHandler()
{
}

int UserDefinedDisplayQueryHandler::Handling()
{
	UserDefinedDisplayQueryStruct * pUserDefinedDisplayQueryStruct = (UserDefinedDisplayQueryStruct *)m_pPacket->GetpData();
	UserDefinedDisplayAckStruct * pUserDefinedDisplayAckStruct = QueryUserDefinedDisplay(pUserDefinedDisplayQueryStruct->m_szUserId);

	Packet packetAck(pUserDefinedDisplayAckStruct->m_dwBufSize, (BYTE *)pUserDefinedDisplayAckStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	delete pUserDefinedDisplayAckStruct;

	return 0;
}

UserDefinedDisplayAckStruct * UserDefinedDisplayQueryHandler::QueryUserDefinedDisplay(const CString& strUserId)
{
	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CUserDefinedDisplaySet rsUserDefinedDisplaySet(pDatabase);

	rsUserDefinedDisplaySet.m_strFilter.Format("UserId = '%s' OR ShareFlag = 1", strUserId);
	rsUserDefinedDisplaySet.Open(AFX_DB_USE_DEFAULT_TYPE, NULL, CRecordset::readOnly);
	std::vector<UserDefinedDisplayItem *> UserDefinedDisplayItems;
	while (!rsUserDefinedDisplaySet.IsEOF())
	{
		UserDefinedDisplayItem * pUserDefinedDisplayItem = new UserDefinedDisplayItem;
		strcpy(pUserDefinedDisplayItem->m_szUserId, rsUserDefinedDisplaySet.m_UserId);
		strcpy(pUserDefinedDisplayItem->m_szDisplayName, rsUserDefinedDisplaySet.m_DisplayName);
		pUserDefinedDisplayItem->m_nShareFlag = rsUserDefinedDisplaySet.m_ShareFlag;

		UserDefinedDisplayItems.push_back(pUserDefinedDisplayItem);

		rsUserDefinedDisplaySet.MoveNext();
	}
	rsUserDefinedDisplaySet.Close();

	int nCount = UserDefinedDisplayItems.size();
	int nBufSize = sizeof(UserDefinedDisplayAckStruct)+sizeof(UserDefinedDisplayItem)*(nCount-1);
	UserDefinedDisplayAckStruct * pUserDefinedDisplayAckStruct = (UserDefinedDisplayAckStruct*)new BYTE[nBufSize];
	pUserDefinedDisplayAckStruct->m_dwMethodType = VCS_MT_QUERYUSERDEFINEDDISPLAY;
	pUserDefinedDisplayAckStruct->m_nMethodAck = VCS_MR_OK;
	pUserDefinedDisplayAckStruct->m_dwNumOfItems = nCount;
	pUserDefinedDisplayAckStruct->m_dwBufSize = nBufSize;
	for (int i = 0;i < nCount;i++)
	{
		UserDefinedDisplayItem * pUserDefinedDisplayItem = UserDefinedDisplayItems[i];
		memcpy(&pUserDefinedDisplayAckStruct->m_UserDefinedDisplayItems[i], pUserDefinedDisplayItem, sizeof(UserDefinedDisplayItem));
		delete pUserDefinedDisplayItem;
	}
	UserDefinedDisplayItems.clear();

	return pUserDefinedDisplayAckStruct;
}